package cn.aylog.realms;

import cn.aylog.pojo.Account;
import cn.aylog.service.IAccountService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component("myRealm")
public class MyRealm extends AuthorizingRealm {

    @Autowired
    private IAccountService accountService;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        UsernamePasswordToken aToken = (UsernamePasswordToken) token;
        Account account = accountService.getAccountByName(aToken.getUsername());
        if(account != null) {
            return new SimpleAuthenticationInfo(account.getUsername(), account.getPassword(), "UP");
        }
        return null;
    }
}
